//
//  GXContentManageCommonController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/3.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXFeatureCommonController.h"
#import "GXFeatureCommonCell.h"
#import "GXFeatureModel.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "GXFeatureCommonDetailController.h"
@interface GXFeatureCommonController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic,strong) UITableView * featureTableView;
@property (nonatomic,strong) NSMutableArray * dataArray;
@property (nonatomic,copy) NSString * sigeStr;
@end
static NSString *const featureCommonCell=@"GXFeatureCommonCell";
@implementation GXFeatureCommonController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleName;
    self.view.backgroundColor = kGlobalbgColor;
    [self creatNavigationRightItem];
    [self.view addSubview:self.featureTableView];
    [self requestData];
}

- (void)requestData
{
    self.dataArray = [NSMutableArray array];
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    if ([self.titleName isEqualToString:@"轮播图"]) {
        self.sigeStr = @"1";
    }else if ([self.titleName isEqualToString:@"特色菜品"])
    {
        self.sigeStr = @"2";
    }else
    {
        self.sigeStr = @"3";
    }
    [MenuContentManager selectFeatureFuctionWithStoreId:storeId sige:self.sigeStr success:^(id responseObject) {
        if (responseObject) {
           self.dataArray = responseObject;
            [self.featureTableView reloadData];
        }
    } fail:^(id responseObject) {
        
    }];
}

- (void)creatNavigationRightItem
{
    UIButton * rightButton = [UIButton setButtonTitleNormal:@"新增" buttonTintColor:[UIColor whiteColor]];
    [rightButton addTarget:self action:@selector(clickRightItemButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}
- (void)clickRightItemButton:(UIButton *)bt
{
    GXFeatureCommonDetailController * featureDetailVC = [[GXFeatureCommonDetailController alloc]init];
    featureDetailVC.signStr = self.sigeStr;
    featureDetailVC.addBlock = ^(NSString * _Nonnull sigeStr) {
        self.sigeStr = sigeStr;
        [self requestData];
    };
    [self.navigationController pushViewController:featureDetailVC animated:YES];
}
#pragma mark -- 创建uitableview
- (UITableView *)featureTableView
{
    if (_featureTableView == nil) {
        _featureTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,15,kUIScreenWidth-20,kUIScreenHeight-Height_TabBar-Height_StatusBar-30) style:UITableViewStyleGrouped];
        _featureTableView.delegate=self;
        _featureTableView.dataSource=self;
        _featureTableView.emptyDataSetDelegate = self;
        _featureTableView.emptyDataSetDelegate = self;
        _featureTableView.sectionHeaderHeight = 5;
        _featureTableView.sectionFooterHeight = 0.01;
        _featureTableView.showsHorizontalScrollIndicator=NO;
        _featureTableView.showsVerticalScrollIndicator=NO;
       _featureTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _featureTableView.backgroundColor = [UIColor whiteColor];
        _featureTableView.layer.masksToBounds = YES;
        _featureTableView.layer.cornerRadius = 5;
        [_featureTableView registerNib:[UINib nibWithNibName:@"GXFeatureCommonCell" bundle:nil] forCellReuseIdentifier:featureCommonCell];
    }
    return _featureTableView;
}


#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXFeatureCommonCell * cell = [tableView dequeueReusableCellWithIdentifier:featureCommonCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GXFeatureModel * model = self.dataArray[indexPath.section];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}


#pragma DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"common_NullBG"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂未特色菜品";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f],
                                 NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}


@end
